package com.graduation.adminservice.listener;

import com.graduation.common.constans.MqConstants;
import com.graduation.pojo.po.LegalDocument;
import com.graduation.service.ElasticSearchService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LegalDocumentMessageListener {

    @Resource
    private ElasticSearchService elasticSearchService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstants.EXCHANGE.LIKED_TOPIC_EXCHANGE, type = ExchangeTypes.TOPIC),
            value = @Queue(value = MqConstants.QUEUE.LEGAL_DOCUMENT_SAVE_QUEUE),
            key = MqConstants.KEY.LEGAL_DOCUMENT_SAVE_KEY
    ))
    public void handleEsSaveLegalDocument(LegalDocument legalDocument) {
        log.info("收到添加法律文档到es的消息，消息内容：{}", legalDocument);
        elasticSearchService.handleEsSaveLegalDocument(legalDocument);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstants.EXCHANGE.LIKED_TOPIC_EXCHANGE, type = ExchangeTypes.TOPIC),
            value = @Queue(value = MqConstants.QUEUE.LEGAL_DOCUMENT_UPDATE_QUEUE),
            key = MqConstants.KEY.LEGAL_DOCUMENT_UPDATE_KEY
    ))
    public void handleEsUpdateLegalDocument(LegalDocument legalDocument) {
        log.info("收到修改法律文档到es的消息，消息内容：{}", legalDocument);
        elasticSearchService.handleEsUpdateLegalDocument(legalDocument);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstants.EXCHANGE.LIKED_TOPIC_EXCHANGE, type = ExchangeTypes.TOPIC),
            value = @Queue(value = MqConstants.QUEUE.LEGAL_DOCUMENT_DELETE_QUEUE),
            key = MqConstants.KEY.LEGAL_DOCUMENT_DELETE_KEY
    ))
    public void handleEsDeleteLegalDocument(Long id) {
        log.info("收到删除es法律文档到的消息，id：{}", id);
        elasticSearchService.handleEsDeleteLegalDocument(id);
    }

}
